<?php
	session_start();
	
	if (!isset($_SESSION['sesi_login']))
		header("Location:../../index.php");

	@chdir("../../");
	define('ABS_URL',str_replace("\\","/",getcwd()));
	require ABS_URL.'/engine/Smarty.class.php';
	require ABS_URL."/functions/class.query.inc.php";
	$smarty = new Smarty();
	require ABS_URL."/dev_con.inc.php";
	require ABS_URL."/functions/func_file.php";
	$queryData = new queryData();
	$queryData->baseConfig();
	require ABS_URL."/functions/func_content.php";
	require ABS_URL."/langs/".BASE_LANGUAGE.'.inc.php';
	require $queryData->get_curr_dir(dirname(__FILE__))."/langs/".BASE_LANGUAGE.'.inc.php';

	$judul = trim($_POST['judul']);
	$header_c = trim($_POST['isi']);
	$isi = trim($_POST['isi']);
	$author = strtolower($_SESSION['author']);
	$subkategori = $_POST['subkategori'];
	$tipe = "dinamis-".$subkategori;
	$err = "";
	$tanggal = date("Y-m-d-H-i-s");
	if ($_POST['publish']=="ya") {
		$status = 1;
	} else {
		$status = 0;
	}

	if (strtolower(ACT_VER)=="hapus") {
		$kode = (int) $_GET['kode'];
		//Delete Record
		$sql = "DELETE FROM tbl_content WHERE idx='".$kode."' AND tipe LIKE 'dinamis-%'";
		$qry = mysql_query($sql);
		
		($qry) ? $_SESSION['sesi_ver'] = $queryData->cekAksi($kode,'delete') : $_SESSION['sesi_ver'] = '';
		$act = 'lihat';
	} else {
		$a = array($judul,$isi);
		$b = array('empty','empty');
		$c = array($MJ_TITLE,$MJ_CONTENT);
		switch (strtolower($_POST['act_ver'])) {
			case "input" :								
				$result = $queryData->NotificationCheck($a,$b,$c);
				if (empty($result)) {
					//Insert tbl_content
					$sql = "INSERT INTO tbl_content VALUES(NULL,'".$judul."','".$header_c."','".$isi."','".$author."','".$tanggal."','".$tipe."','".$status."')";
					$qry = mysql_query($sql);
					
					$sql = "SELECT LAST_INSERT_ID() as idx FROM tbl_content";
					$qry = mysql_query($sql);
					$row = mysql_fetch_object($qry);
					
					//Update URL direktori
					if ($_POST['url']=="1") {
						$sql = "UPDATE ikadir_kat SET url='index.php/1/dinamis/".$row->idx."' WHERE id_dirkat='".$subkategori."'";
						$qry = mysql_query($sql);
					}
					$_SESSION['sesi_ver'] = $queryData->cekAksi($judul,'add');
					$act = 'lihat';
				} else {
					$_SESSION['sesi_ver'] = $DEV_ACTION_NOTIFICATION;
					foreach($result as $data) {
						$_SESSION['sesi_ver'] .= '<br />- '.$data;
					}
					$act = 'tambah';
				}
			break;
			case "edit" :	
				$kode = (int) $_POST['kode'];
				$result = $queryData->NotificationCheck($a,$b,$c);
				if (empty($result)) {
					$sql = "UPDATE tbl_content SET judul='".$judul."', header_c='".$header_c."', isi='".$isi."', status='".$status."', tipe='".$tipe."' WHERE idx='".$kode."' AND tipe LIKE 'dinamis-%'";
					$qry = mysql_query($sql);
											
					//Update URL direktori
					if ($_POST['url']=="1") {
						$sql = "UPDATE ikadir_kat SET url='index.php/1/dinamis/".$kode."' WHERE id_dirkat='".$subkategori."'";
						$qry = mysql_query($sql);
					}
					$_SESSION['sesi_ver'] = $queryData->cekAksi($judul,'edit');
					$act = 'lihat';
				} else {
					$_SESSION['sesi_ver'] = $DEV_ACTION_NOTIFICATION;
					foreach($result as $data) {
						$_SESSION['sesi_ver'] .= '<br />- '.$data;
					}								
					$act = 'edit&kode='.$kode;
				}
			break;
		}
	}
			
	if (isset($_SESSION['sesi_login'])) {
		header('Location:../../?show='.SHOW.'&showview='.SHOWVIEW.'&act='.$act.'&limitdown=0');
	} else {
		header("Location:../../index.php");
	};
?>